/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.stat.model;

import org.stat.model.data.ModelData;
import org.stat.model.estimation.ModelEstimator;
import org.stat.model.function.ModelFunction;
import org.stat.model.parameter.ParamNotEstimatedException;
import org.stat.model.parameter.Parameter;

import java.util.logging.Logger;

/**
 *
 * @author stewari1
 */
public abstract class Model {

    /** Field description */
    protected static final Logger logger = Logger.getLogger(Model.class.getName());

    /** Field description */
    private boolean setUpChanged = false;

    /** Field description */
    private boolean modelRun = false;

    /** Field description */
    private String name;

    /** Field description */
    private Parameter[] parameters;

    /** Field description */
    private ModelData modelData;

    /** Field description */
    private ModelEstimator modelEstimator;

    /** Field description */
    private ModelFunction modelFunction;

    /**
     * Constructs ...
     */
    protected Model() {}

    /**
     * subclasses are expected to override this by doing their class specific activities and then
     * calling this at the end. For example, <code>ModelEstimator</code> typically need fresh data statistics
     *  and the model should compute that first before running this method. Note that
     * <code>{@link #setUpChanged()}</code> could have been caused by a fixed number of variants and it is the
     * model`s responsibility to make sure that computations are not performed on stale data.
     * @throws NullPointerException
     */
    protected void runModel() {

        if (isSetUpChanged() ||!isModelRun()) {

            try {

                modelEstimator.estimate();
                modelFunction.compute(modelData.getRecords());

                this.modelRun     = true;
                this.setUpChanged = false;

            } catch (ParamNotEstimatedException ex) {
                logger.severe(ex.getMessage());
            }
        }
    }

    public void setModelEstimator(ModelEstimator modelEstimator) {

        this.modelEstimator = modelEstimator;

        this.modelEstimator.setModel(this);
        setUpChanged();
    }

    void setUpChanged() {
        this.setUpChanged = true;
    }

    protected void setModelFunction(ModelFunction modelFunction) {

        this.modelFunction = modelFunction;

        this.modelFunction.setModel(this);
        setUpChanged();
    }

    protected void setName(String name) {
        this.name = name;
    }

    protected void setParameters(Parameter[] parameters) {
        this.parameters = parameters;
    }

    boolean isSetUpChanged() {
        return setUpChanged;
    }

    boolean isModelRun() {
        return modelRun;
    }

    public ModelData getModelData() {
        return modelData;
    }

    protected void setModelData(ModelData modelData) {

        this.modelData = modelData;

        setUpChanged();
    }

    public Parameter[] getParameters() {
        return parameters;
    }

    public String getName() {
        return name;
    }
}
